import {
    AfterContentChecked, AfterViewChecked, AfterViewInit, Component, ElementRef, Input, OnChanges, OnDestroy, OnInit,
    ViewChild
} from '@angular/core';
import { ServerSocket } from '@core/services/ws.service';

@Component({
  selector: 'app-c-six-hat',
  templateUrl: './c-six-hat.component.html',
  styleUrls: ['./c-six-hat.component.less']
})
export class CSixHatComponent implements OnInit, AfterViewChecked, AfterViewInit, OnDestroy, OnChanges, AfterContentChecked {
    @ViewChild('top') top: ElementRef;
    @ViewChild('center') center: ElementRef;
    @ViewChild('all') all: ElementRef;

    @ViewChild('answerListBox') answerListBox: ElementRef;
    @Input() data;
    @Input() socket;
    @Input() IPPORT;
    @Input() goldObj;
    @Input() sixScore = false;
    @Input() sixScoreList = [];

    showScore = false;
    sortAnswer(list) {
        this.showScore = true;
        for (const item of list) {
            for (let i = 0; i < this._answerList.length; i++ ) {
                if (item.g_id_two === this._answerList[i].g_id_two) {
                    this._answerList[i].score = item.score;
                }
            }
        }
    }
    // 测试用
    // time;

    _answerList = [];
    // 正式时为父组件socket接收数据 @Input()至此组件;
    @Input()
    set answerList(value) {
        this._answerList = value;
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }

    get answerList() {
        return this._answerList;
    }
    constructor(public ws: ServerSocket, public ref: ElementRef) { }

    ngOnInit() {
    }

    ngAfterViewChecked() {
        // console.log('_________***********');
        // console.log(this.goldObj);
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }


    ngAfterViewInit() {
        // if (!this.socket) {
        //     // 测试用
        //     this.time = setInterval(() => {
        //         this.answerList.push({
        //             answer: '回答',
        //             name: 'user'
        //         });
        //         console.log(this.answerListBox.nativeElement.offsetHeight);
        //         this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
        //     }, 5000);
        // }
    }

    ngOnDestroy() {
        // clearInterval(this.time);
    }

    ngAfterContentChecked() {
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }
    ngOnChanges() {
        this.center.nativeElement.scrollTop = this.answerListBox.nativeElement.offsetHeight - this.center.nativeElement.offsetHeight;
    }
}
